package com.ltkj.ticket.service.impl;

import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicVenuesProgramme;
import com.ltkj.ticket.domain.TicVenuesProgrammePeriod;
import com.ltkj.ticket.mapper.TicVenuesProgrammeBusinessMapper;
import com.ltkj.ticket.mapper.TicVenuesProgrammeMapper;
import com.ltkj.ticket.service.ITicVenuesProgrammeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 场馆方案Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-05
 */
@Service
public class TicVenuesProgrammeServiceImpl implements ITicVenuesProgrammeService 
{
    @Autowired
    private TicVenuesProgrammeMapper ticVenuesProgrammeMapper;
    @Autowired
    private TicVenuesProgrammeBusinessMapper ticVenuesProgrammeBusinessMapper;

    /**
     * 查询场馆方案
     * 
     * @param venuesProgrammeId 场馆方案主键
     * @return 场馆方案
     */
    @Override
    public TicVenuesProgramme selectTicVenuesProgrammeByVenuesProgrammeId(Long venuesProgrammeId)
    {
        return ticVenuesProgrammeMapper.selectTicVenuesProgrammeByVenuesProgrammeId(venuesProgrammeId);
    }

    /**
     * 查询场馆方案列表
     * 
     * @param ticVenuesProgramme 场馆方案
     * @return 场馆方案
     */
    @Override
    @DataScope(storeAlias = "g")
    public List<TicVenuesProgramme> selectTicVenuesProgrammeList(TicVenuesProgramme ticVenuesProgramme)
    {
        return ticVenuesProgrammeMapper.selectTicVenuesProgrammeList(ticVenuesProgramme);
    }

    /**
     * 新增场馆方案
     * 
     * @param ticVenuesProgramme 场馆方案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTicVenuesProgramme(TicVenuesProgramme ticVenuesProgramme)
    {
        ticVenuesProgramme.setCreateTime(DateUtils.getNowDate());
        ticVenuesProgramme.setIsDel("0");
        String username = SecurityUtils.getUsername();
        ticVenuesProgramme.setCreateBy(username);
        int rows = ticVenuesProgrammeMapper.insertTicVenuesProgramme(ticVenuesProgramme);
        insertTicVenuesProgrammePeriod(ticVenuesProgramme);
        return rows;
    }

    /**
     * 修改场馆方案
     * 
     * @param ticVenuesProgramme 场馆方案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTicVenuesProgramme(TicVenuesProgramme ticVenuesProgramme)
    {
        ticVenuesProgramme.setUpdateTime(DateUtils.getNowDate());
        ticVenuesProgramme.setIsDel("0");
        String username = SecurityUtils.getUsername();
        ticVenuesProgramme.setUpdateBy(username);
        ticVenuesProgrammeMapper.deleteTicVenuesProgrammePeriodByVenuesProgrammeId(ticVenuesProgramme.getVenuesProgrammeId());
        insertTicVenuesProgrammePeriod(ticVenuesProgramme);

        return ticVenuesProgrammeMapper.updateTicVenuesProgramme(ticVenuesProgramme);
    }

    /**
     * 批量删除场馆方案
     * 
     * @param venuesProgrammeIds 需要删除的场馆方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTicVenuesProgrammeByVenuesProgrammeIds(Long[] venuesProgrammeIds)
    {
        ticVenuesProgrammeMapper.deleteTicVenuesProgrammePeriodByVenuesProgrammeIds(venuesProgrammeIds);
        return ticVenuesProgrammeMapper.deleteTicVenuesProgrammeByVenuesProgrammeIds(venuesProgrammeIds);
    }

    /**
     * 删除场馆方案信息
     * 
     * @param venuesProgrammeId 场馆方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTicVenuesProgrammeByVenuesProgrammeId(Long venuesProgrammeId)
    {
        ticVenuesProgrammeMapper.deleteTicVenuesProgrammePeriodByVenuesProgrammeId(venuesProgrammeId);
        return ticVenuesProgrammeMapper.deleteTicVenuesProgrammeByVenuesProgrammeId(venuesProgrammeId);
    }

    @Override
    public List<TicVenuesProgramme> selectTicVenuesProgrammeSelect(Long ticOtherBaseId) {
        return ticVenuesProgrammeBusinessMapper.listSelect(ticOtherBaseId);
    }

    /**
     * 新增方案时间段信息
     * 
     * @param ticVenuesProgramme 场馆方案对象
     */
    public void insertTicVenuesProgrammePeriod(TicVenuesProgramme ticVenuesProgramme)
    {
        List<TicVenuesProgrammePeriod> ticVenuesProgrammePeriodList = ticVenuesProgramme.getTicVenuesProgrammePeriodList();
        Long venuesProgrammeId = ticVenuesProgramme.getVenuesProgrammeId();
        if (StringUtils.isNotNull(ticVenuesProgrammePeriodList))
        {
            String username = SecurityUtils.getUsername();
            List<TicVenuesProgrammePeriod> list = new ArrayList<TicVenuesProgrammePeriod>();
            for (TicVenuesProgrammePeriod ticVenuesProgrammePeriod : ticVenuesProgrammePeriodList)
            {
                ticVenuesProgrammePeriod.setVenuesProgrammeId(venuesProgrammeId);
                ticVenuesProgrammePeriod.setCreateTime(DateUtils.getNowDate());
                ticVenuesProgrammePeriod.setIsDel("0");

                ticVenuesProgrammePeriod.setCreateBy(username);
                list.add(ticVenuesProgrammePeriod);
            }
            if (list.size() > 0)
            {
                ticVenuesProgrammeMapper.batchTicVenuesProgrammePeriod(list);
            }
        }
    }
}
